<?php

    function __autoload($class_name) 
    {
        include $class_name . '.php';
    }
    $page = new page('Sprava uzivatelu');
    $content="";
    if(isset($_SESSION['user'])&& 
            $_SESSION['user']->validate() && 
            ($_SESSION['user']->getRights()==3))
    {
        //TODO: stranka pro admina
        $content .= 'Jste prihlasen jako admin pod loginem : '. $_SESSION['user']->getLogin() . '<br />';
        $users = new table(array(
            '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=id">id</a>',
            '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=login">login</a>',
            '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=name">name</a>',
            '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=email">email</a>',
            '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=rights">rights</a>',
            '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=state">state</a>'));
        $db_conn = new dbConnection();        
        if(isset($_GET['page']))
        {
            $page_num = $_GET['page'];
        }  else {
            $page_num = 1;
        }
        if(isset($_GET['order_by']))
            {
                $order_by = 'ORDER BY '.$_GET['order_by'].' ASC';
            }
            else
            {
                $order_by = '';
            }
        $sql_result = $db_conn->db_query('SELECT COUNT(id) AS row_count FROM authors');
        $row = mysql_fetch_array($sql_result,MYSQL_ASSOC);
        $row_count = (int)$row['row_count'];
        $sql_result = $db_conn->db_query('SELECT id, login, name, email, rights, state FROM authors '.$order_by.' LIMIT '.(($page_num-1)*5) .',5');        
        while($row = mysql_fetch_array($sql_result,MYSQL_NUM))
        {
            array_push($row, "<a href=editUser.php?id=$row[0]>zmenit</a>");
            $users->addRow($row);            
        }        
        $content .= $users->render();
        $paging = new paging( $row_count, $page_num, 5);
        $content .= $paging->render();
    }else
    {
        //stranka pro neadmina
        //vyhodit
        $content .= 'nemate dostatecna opravneni!<br />';
    }
    echo $page->render($content);
?>
